var database = require('../../../data/data.js');
Page({

  data: {
    contents: "",
    img1: "/image/icon/star.png",
    img2: "/image/icon/star1.png",
    flag: true,
    flag1: false,
    flag2: true,
    flag3: true,
    star1: true,
    star2: false,
    star3: false,
    star4: false,
    star5: false,
    comment1: false,
    color1: "rgb(200, 200, 200)",
    color2: "rgb(200, 200, 200)",
    color3: "rgb(200, 200, 200)",
    color4: "rgb(200, 200, 200)",
    color5: "rgb(200, 200, 200)",
    color6: "rgb(200, 200, 200)",
    loading: true,
    pageNum: 1,
    noData: false,
  },

  onLoad: function () {
    var that = this;
    if (database.login == false) {
      wx.redirectTo({
        url: '/pages/newBinding/newBinding',
      })
    } else {
      wx.showLoading({
        title: '资源加载中',
        mask: true
      })
      var that = this;
      wx.request({
        url: database.url + '/project/getMyYuzhanByOpenid.action',
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        },
        data: {
          openid: database.userId,
          pageNum: that.data.pageNum
        },
        success(res) {
          var date1 = new Date();
          var contents = res.data;
          var teams = [];
          var index = 0;
          var contentss = [];
          var noData = false;
          if (contents.length < 4) {
            noData = true;
          } else {
            noData = false;
            that.data.pageNum++;
          }
          wx.hideLoading();
          that.setData({
            loading: false
          })
          for (var i = 0; i < contents.length; i++) {
            var flag = true;
            var reg = new RegExp("-", "g");
            var newTime = contents[i].postdate.replace(reg, "/");
            var date = new Date(newTime);
            var reg1 = new RegExp("-", "g");
            var newTime1 = contents[i].date.replace(reg1, "/");
            var date2 = new Date(newTime1);
            if (date2 - date1 > 0) {
              if (parseInt((date2 - date1) / 1000) >= 60) {
                if (parseInt((date2 - date1) / 1000 / 60) >= 60) {
                  if (parseInt((date2 - date1) / 1000 / 60 / 60) >= 48) {
                    flag = true;
                  } else {
                    flag = false;
                  }
                } else {
                  flag = false;
                }
              } else {
                flag = false;
              }
            } else {
              flag = false;
            }
            var seconds = 0;
            var minutes = 0;
            var hour = 0;
            var day = 0;
            var month = 0;
            var year = 0;
            var timeDifference = "";
            if ((date1 - date) / 1000 >= 60) {
              if ((date1 - date) / 1000 / 60 >= 60) {
                if ((date1 - date) / 1000 / 60 / 60 >= 24) {
                  if ((date1 - date) / 1000 / 60 / 60 / 24 >= 30) {
                    if ((date1 - date) / 1000 / 60 / 60 / 24 / 30 >= 12) {
                      year = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30 / 12);
                      timeDifference = year + "年前";
                    } else {
                      month = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30);
                      timeDifference = month + "个月前";
                    }
                  } else {
                    day = parseInt((date1 - date) / 1000 / 60 / 60 / 24);
                    timeDifference = day + "天前";
                  }
                } else {
                  hour = parseInt((date1 - date) / 1000 / 60 / 60);
                  timeDifference = hour + "小时前";
                }
              } else {
                minutes = parseInt((date1 - date) / 1000 / 60);
                timeDifference = minutes + "分钟前";
              }
            } else {
              seconds = parseInt((date1 - date) / 1000);
              timeDifference = seconds + "秒前";
            }
            var content = {
              address: contents[i].adress,
              cost: contents[i].cost,
              date: contents[i].date,
              distance: contents[i].distance,
              gameid: contents[i].gameid,
              person: contents[i].person,
              pteamid: contents[i].pteamid,
              popenid: contents[i].popenid,
              mteamid: contents[i].mteamid,
              mopenid: contents[i].mopenid,
              sort: contents[i].sort,
              time: contents[i].time,
              week: contents[i].week,
              status: contents[i].status,
              teamLogo: contents[i].pteamlogo,
              title: contents[i].pteamname,
              postdate: contents[i].postdate,
              timeDifference: timeDifference,
              cancle: flag,
              comment: contents[i].comment
            }
            contentss[i] = content;
          }

          that.setData({
            contents: contentss,
            noData: noData
          })
        }
      })
    }
  },
  onReachBottom: function () {
    var that = this;
    if (!this.data.noData) {
      wx.showLoading({
        title: '资源加载中',
        mask: true
      })
      wx.request({
        url: database.url + '/project/getMyYuzhanByOpenid.action',
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        },
        data: {
          openid: database.userId,
          pageNum: that.data.pageNum
        },
        success(res) {
          var date1 = new Date();
          var contents = res.data;
          var teams = [];
          var index = 0;
          var contentss = [];
          var contentsss = [];
          var noData = false;
          if (contents.length < 4) {
            noData = true;
          } else {
            noData = false;
            that.data.pageNum++;
          }
          wx.hideLoading();
          that.setData({
            loading: false
          })
          for (var i = 0; i < contents.length; i++) {
            var flag = true;
            var reg = new RegExp("-", "g");
            var newTime = contents[i].postdate.replace(reg, "/");
            var date = new Date(newTime);
            var reg1 = new RegExp("-", "g");
            var newTime1 = contents[i].date.replace(reg1, "/");
            var date2 = new Date(newTime1);
            if (date2 - date1 > 0) {
              if (parseInt((date2 - date1) / 1000) >= 60) {
                if (parseInt((date2 - date1) / 1000 / 60) >= 60) {
                  if (parseInt((date2 - date1) / 1000 / 60 / 60) >= 48) {
                    flag = true;
                  } else {
                    flag = false;
                  }
                } else {
                  flag = false;
                }
              } else {
                flag = false;
              }
            } else {
              flag = false;
            }
            var seconds = 0;
            var minutes = 0;
            var hour = 0;
            var day = 0;
            var month = 0;
            var year = 0;
            var timeDifference = "";
            if ((date1 - date) / 1000 >= 60) {
              if ((date1 - date) / 1000 / 60 >= 60) {
                if ((date1 - date) / 1000 / 60 / 60 >= 24) {
                  if ((date1 - date) / 1000 / 60 / 60 / 24 >= 30) {
                    if ((date1 - date) / 1000 / 60 / 60 / 24 / 30 >= 12) {
                      year = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30 / 12);
                      timeDifference = year + "年前";
                    } else {
                      month = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30);
                      timeDifference = month + "个月前";
                    }
                  } else {
                    day = parseInt((date1 - date) / 1000 / 60 / 60 / 24);
                    timeDifference = day + "天前";
                  }
                } else {
                  hour = parseInt((date1 - date) / 1000 / 60 / 60);
                  timeDifference = hour + "小时前";
                }
              } else {
                minutes = parseInt((date1 - date) / 1000 / 60);
                timeDifference = minutes + "分钟前";
              }
            } else {
              seconds = parseInt((date1 - date) / 1000);
              timeDifference = seconds + "秒前";
            }
            var content = {
              address: contents[i].adress,
              cost: contents[i].cost,
              date: contents[i].date,
              distance: contents[i].distance,
              gameid: contents[i].gameid,
              person: contents[i].person,
              pteamid: contents[i].pteamid,
              popenid: contents[i].popenid,
              mteamid: contents[i].mteamid,
              mopenid: contents[i].mopenid,
              sort: contents[i].sort,
              time: contents[i].time,
              week: contents[i].week,
              status: contents[i].status,
              teamLogo: contents[i].pteamlogo,
              title: contents[i].pteamname,
              postdate: contents[i].postdate,
              timeDifference: timeDifference,
              cancle: flag,
              comment: contents[i].comment
            }
            contentss[i] = content;
          }
          for (var i = 0; i < that.data.contents.length + contentss.length; i++) {
            if (i < that.data.contents.length) {
              contentsss[i] = that.data.contents[i];
            } else {
              contentsss[i] = contentss[i - that.data.contents.length];
            }
          }
          that.setData({
            contents: contentsss,
            noData: noData
          })
        }
      })
    }
  },
  toBattle: function () {
    wx.redirectTo({
      url: '/pages/my/my_battle_fight/my_battle_fight',
    })
  },
  cancelGame: function (e) {
    if (e.currentTarget.dataset.cancle) {
      this.setData({
        flag3: false,
        gameid: e.currentTarget.dataset.gameid,
        mopenid: e.currentTarget.dataset.mopenid,
        popenid: e.currentTarget.dataset.popenid
      })
    } else {
      wx.showModal({
        content: '赛事马上就要开始，您不能取消赛事！',
      })
    }
  },
  delete: function (e) {
    wx.showModal({
      content: '删除该约战？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: database.url + '/project/uploadDeleteYuezhan.action',
            data: {
              id: e.currentTarget.dataset.gameid,
            },
            method: "POST",
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(res) {
              if (res.data == 0) {
                wx.redirectTo({
                  url: '/pages/my/my_add_fight/my_add_fight',
                })
              } else {
                wx.showToast({
                  title: '删除失败!',
                })
              }
            }
          });
        }
      }
    })
  },
  cancelYuezhan: function (e) {
    var that = this;
    if (database.userId == that.data.mopenid) {
      var mopenid = that.data.popenid
    } else {
      var mopenid = that.data.mopenid
    }
    wx.request({
      url: database.url + '/project/uploadCancelYuezhan.action',
      data: {
        id: that.data.gameid,
        popenid: database.userId,
        mopenid: mopenid,
        reasons: that.data.reason
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        that.setData({
          flag3: true,
        })
        if (res.data == 0) {
          wx.showToast({
            title: '发送成功!',
          })
        } if (res.data == -2) {
          wx.showToast({
            title: '您已通知过了!',
          })
        }
      }
    });
  },
  changeStar1: function (e) {
    this.setData({
      star1: true,
      star2: false,
      star3: false,
      star4: false,
      star5: false,
    })
  },
  changeStar2: function (e) {
    this.setData({
      star1: true,
      star2: true,
      star3: false,
      star4: false,
      star5: false
    })
  },
  changeStar3: function (e) {
    this.setData({
      star1: true,
      star2: true,
      star3: true,
      star4: false,
      star5: false
    })
  },
  changeStar4: function (e) {
    this.setData({
      star1: true,
      star2: true,
      star3: true,
      star4: true,
      star5: false
    })
  },
  changeStar5: function (e) {
    this.setData({
      star1: true,
      star2: true,
      star3: true,
      star4: true,
      star5: true
    })
  },
  changeColor1: function (e) {
    if (this.data.color1 == "rgb(200, 200, 200)") {
      this.setData({
        color1: "#259b23",
        other1: e.currentTarget.dataset.other
      })
    } else {
      this.setData({
        color1: "rgb(200, 200, 200)",
        other1: ''
      })
    }

  },
  changeColor2: function (e) {
    if (this.data.color2 == "rgb(200, 200, 200)") {
      this.setData({
        color2: "#259b23",
        other2: e.currentTarget.dataset.other
      })
    } else {
      this.setData({
        color2: "rgb(200, 200, 200)",
        other2: ''
      })
    }
  },
  changeColor3: function (e) {
    if (this.data.color3 == "rgb(200, 200, 200)") {
      this.setData({
        color3: "#259b23",
        other3: e.currentTarget.dataset.other
      })
    } else {
      this.setData({
        color3: "rgb(200, 200, 200)",
        other3: ''
      })
    }
  },
  changeColor4: function (e) {
    if (this.data.color4 == "rgb(200, 200, 200)") {
      this.setData({
        color4: "#259b23",
        other4: e.currentTarget.dataset.other
      })
    } else {
      this.setData({
        color4: "rgb(200, 200, 200)",
        other4: ''
      })
    }
  },
  changeColor5: function (e) {
    if (this.data.color5 == "rgb(200, 200, 200)") {
      this.setData({
        color5: "#259b23",
        other5: e.currentTarget.dataset.other
      })
    } else {
      this.setData({
        color5: "rgb(200, 200, 200)"
      })
    }
  },
  changeColor6: function (e) {
    if (this.data.color6 == "rgb(200, 200, 200)") {
      this.setData({
        color6: "#259b23",
        other6: e.currentTarget.dataset.other
      })
    } else {
      this.setData({
        color6: "rgb(200, 200, 200)",
        other6: ''
      })
    }
  },
  textareaChange: function (e) {
    if (e.detail.value != "") {
      this.setData({
        flag1: true
      })
      this.data.comment.detail = e.detail.value;
    } else {
      this.setData({
        flag1: false
      })
      this.data.comment.detail = '';
    }
  },
  textareaChange1: function (e) {
    if (e.detail.value != "") {
      this.setData({
        flag4: true,
        reason: e.detail.value
      })
    } else {
      this.setData({
        flag4: false,
        reason: ""
      })
    }
  },
  submit: function () {
    var that = this;
    var star = '';
    if (this.data.star5) {
      star = 5;
    } else if (this.data.star4) {
      star = 4;
    } else if (this.data.star3) {
      star = 3;
    } else if (this.data.star2) {
      star = 2;
    } else if (this.data.star1) {
      star = 1;
    }
    var other = '';
    if (this.data.other1 != undefined && this.data.other1 != '') {
      other = this.data.other1;
      if (this.data.other2 != undefined && this.data.other2 != '') {
        other = other + " " + this.data.other2;
        if (this.data.other3 != undefined && this.data.other3 != '') {
          other = other + " " + this.data.other3;
          if (this.data.other4 != undefined && this.data.other4 != '') {
            other = other + " " + this.data.other4;
            if (this.data.other5 != undefined && this.data.other5 != '') {
              other = other + " " + this.data.other5;
            }
          }
        }
      }
    }
    this.data.comment.star = star;
    this.data.comment.other = other;
    wx.request({
      url: database.url + '/project/uploadComment.action',
      data: {
        openid: this.data.comment.openid,
        matchid: this.data.comment.matchid,
        teamid: this.data.comment.mteamid,
        star: this.data.comment.star,
        detail: this.data.comment.detail,
        other: this.data.comment.other,
        pteamid: this.data.comment.pteamid
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.data.result == 0) {
          that.setData({
            flag2: false,
            flag: true
          })
        }
      }
    })
  },
  cancel: function () {
    this.setData({
      flag: true
    })
  },
  update: function (e) {
    var gameid = e.currentTarget.dataset.gameid;
    var popenid = e.currentTarget.dataset.popenid;
    wx.navigateTo({
      url: '/pages/fight/update/update?gameid=' + gameid + '&popenid' + popenid,
    })
  },
  detail: function (e) {
    var status = e.currentTarget.dataset.status;
    var gameid = e.currentTarget.dataset.gameid;
    var popenid = e.currentTarget.dataset.popenid;
    var mopenid = e.currentTarget.dataset.mopenid;
    if (status == "0") {
      wx.navigateTo({
        url: '/pages/fight_detail/battle/battle?gameid=' + gameid + '&popenid=' + popenid,
      })
    } else if (status == "1" || status == "2") {
      wx.navigateTo({
        url: '/pages/fight_detail/publisher/publisher?gameid=' + gameid + '&popenid=' + popenid,
      })
    }
  },
  comment: function (e) {
    if (e.currentTarget.dataset.comment == 0) {
      var comm = {
        mopenid: e.currentTarget.dataset.mopenid,
        openid: database.userId,
        matchid: e.currentTarget.dataset.gameid,
        mteamid: e.currentTarget.dataset.mteamid,
        pteamid: e.currentTarget.dataset.pteamid,
        detail: '',
        other: '',
        star: ''
      }
      this.setData({
        flag: false,
        comment: comm
      })
    } else {
      wx.showToast({
        title: '您已经评论过了',
        icon: 'success',
      })
    }
  },
  toTeamDetail: function (e) {
    var teamid = e.currentTarget.dataset.teamid;
    var userid = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '/pages/my/myteam/mainpage1/mainpage1?id=' + teamid + '&userid=' + userid,
    })
  },
  timeDifferenceTime: function (date1, postdate) {

    return timeDifference;
  },
  backtoyuezhan: function () {
    wx.redirectTo({
      url: '/pages/my/my_add_fight/my_add_fight',
    })
  },
  pinglun: function (e) {
    var teamid = e.currentTarget.dataset.teamid;
    var userid = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '/pages/my/myteam/mainpage1/mainpage1?id=' + teamid + '&userid=' + userid,
    })
  },
  cancel1: function (e) {
    this.setData({
      flag3: true
    })
  }
})